package com.qiangesoft.blog.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiangesoft.blog.common.mvc.PageQuery;
import com.qiangesoft.blog.common.mvc.PageResultVO;
import com.qiangesoft.blog.common.result.ResponseResult;
import com.qiangesoft.blog.system.entity.SysOperationLog;
import com.qiangesoft.blog.system.mapper.SysOperationLogMapper;
import com.qiangesoft.blog.system.service.ISysOperationLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 系统操作日志 服务实现类
 * </p>
 *
 * @author qiange
 * @since 2023-06-01
 */
@Service
public class SysOperationLogServiceImpl extends ServiceImpl<SysOperationLogMapper, SysOperationLog> implements ISysOperationLogService {
    @Override
    public PageResultVO<SysOperationLog> listLog(PageQuery pageQuery, String operation) {
        PageResultVO<SysOperationLog> pageResultVO = new PageResultVO<>();
        Integer pageNum = pageQuery.getPageNum();
        Integer pageSize = pageQuery.getPageSize();
        pageResultVO.setPageNum(pageNum);
        pageResultVO.setPageSize(pageSize);

        LambdaQueryWrapper<SysOperationLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(operation), SysOperationLog::getOperationName, operation)
                .orderByDesc(SysOperationLog::getCreateTime);
        Page<SysOperationLog> page = baseMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        pageResultVO.setTotal(page.getTotal());
        pageResultVO.setPages(page.getPages());
        pageResultVO.setResults(page.getRecords());
        return pageResultVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteLog(List<Long> ids) {
        int rows = baseMapper.deleteBatchIds(ids);
        return rows > 0 ? ResponseResult.success() : ResponseResult.error("批量删除操作日志失败");
    }
}
